package tools;

import funcktion.UserManage;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class UserAddGUI extends JFrame {
    private JTextField userIdTextField;
    private JTextField userNameTextField;
    private JTextField genderTextField;
    private JTextField BorrowedBooksTextField;
    private JTextField maxBorrowedBooksTextField;
    private JTextField statusTextField;
    private JButton saveButton;

    public UserAddGUI() {
        setTitle("读者信息");
//        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new GridLayout(7, 2, 10, 10));

        JLabel userIdLabel = new JLabel("用户证号");
        userIdTextField = new JTextField();
        JLabel userNameLabel = new JLabel("用户名称");
        userNameTextField = new JTextField();
        JLabel genderLabel = new JLabel("性别");
        genderTextField = new JTextField();
        JLabel BorrowedBooksLabel = new JLabel("借书量");
        BorrowedBooksTextField = new JTextField();
        JLabel maxBorrowedBooksLabel = new JLabel("可借最大数量");
        maxBorrowedBooksTextField = new JTextField();
        JLabel statusLabel = new JLabel("状态");
        statusTextField = new JTextField();

        saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveUserInfo();
            }
        });

        add(userIdLabel);
        add(userIdTextField);
        add(userNameLabel);
        add(userNameTextField);
        add(genderLabel);
        add(genderTextField);
        add(BorrowedBooksLabel);
        add(BorrowedBooksTextField);
        add(maxBorrowedBooksLabel);
        add(maxBorrowedBooksTextField);
        add(statusLabel);
        add(statusTextField);
        add(new JLabel());
        add(saveButton);

        pack();
        setLocationRelativeTo(null);
    }


    public boolean isTenDigitNumber(String str){
        // 使用正则表达式匹配 9 位数字
        String regex = "\\d{9}";
        return str.matches(regex);
    }
    private void saveUserInfo() {
        String userId = userIdTextField.getText();
        String userName = userNameTextField.getText();
        String gender = genderTextField.getText();
        int borrowedBooks = 0;
        int maxBorrowedBooks = 0;
        String status = statusTextField.getText();




        if (isTenDigitNumber(userId) == false){
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。\n图书证号必须为9位数字","警告" ,JOptionPane.WARNING_MESSAGE);
        }else if (gender.isEmpty() || userName.isEmpty() || status.isEmpty()) {
            JOptionPane.showMessageDialog(this, "请填写所有必填字段。");
        }else {

            try {
                borrowedBooks = Integer.parseInt(BorrowedBooksTextField.getText());
                maxBorrowedBooks = Integer.parseInt(maxBorrowedBooksTextField.getText());

                // 保存图书信息到数据库
                // ...

                // 检查必填字段是否为空

                try {
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/library", "root", "lang520520");
                    PreparedStatement stmt = conn.prepareStatement("INSERT INTO User (userId, userName, gender, borrowedBooks, maxBorrowedBooks, status) VALUES (?, ?, ?, ?, ?, ?)");
                    stmt.setString(1, userId);
                    stmt.setString(2, userName);
                    stmt.setString(3, gender);
                    stmt.setInt(4, borrowedBooks);
                    stmt.setInt(5, maxBorrowedBooks);
                    stmt.setString(6, status);

                    stmt.executeUpdate();

                    JOptionPane.showMessageDialog(this, "读者信息保存成功");

                    UserManage GUI = new UserManage();
                    GUI.setVisible(true);
                    setVisible(false);
                } catch (SQLException e) {
                    JOptionPane.showMessageDialog(this, "保存失败：" + e.getMessage());
                    e.printStackTrace();
                }

            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(this, "请输入有效的数字");
                e.printStackTrace();
            }
        }
    }

}
